package com.pb.wkflow.api.controller;

import com.pb.infra.oth.entity.common.Result;
import com.pb.wkflow.api.controller.request.ProcDefineCreateRequest;
import com.pb.wkflow.api.controller.request.ProcDefineJsonRequest;
import com.pb.wkflow.api.controller.request.ProcDefineQueryRequest;
import com.pb.wkflow.api.controller.request.ProcessInstanceRequest;
import com.pb.wkflow.api.service.ProcDefineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 流程定义API
 * @author 苗益辉
 * @date 2022/9/1 11:20
 */
@Slf4j
@RestController
@RequestMapping("v1/workflow/procDefine")
public class ProcDefineApi {

    @Autowired
    ProcDefineService procDefineService;

    /**
     * 创建流程
     * @param request 请求
     * @return Result
     */
    @PostMapping("/create")
    public Result createModel(@RequestBody ProcDefineCreateRequest request) {
        return procDefineService.createEmptyBpmnModel(request);
    }

    /**
     * 保存流程设计json
     * @param request 请求
     * @return Result
     */
    @PostMapping("/saveModel")
    public Result saveModel(@RequestBody ProcDefineCreateRequest request) {
        log.info("---------- 保存流程设计 ---------- {}", request);
        return procDefineService.saveJsonModel(request);
    }

    /**
     * 部署流程
     * @param request request
     * @return Result
     */
    @PostMapping("/deployModel")
    public Result deployModel(@RequestBody ProcDefineJsonRequest request) {
        log.info("---------- 部署流程 ---------- {}", request);
        return procDefineService.deployModel(request);
    }

    @PostMapping("/saveAndDeployModel")
    public Result saveAndDeployModel(@RequestBody ProcDefineCreateRequest request) {
        log.info("---------- 保存并部署流程 ---------- {}", request);
        return procDefineService.saveAndDeployModel(request);
    }

    /**
     * 获取流程图列表
     * @param pageIndex 页码
     * @param pageSize 页数
     * @param request 请求
     * @return Result
     */
    @PostMapping("/getJsonList")
    public Result getJsonList(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                              @RequestBody ProcDefineJsonRequest request) {
        log.info("---------- 获取流程图列表 ---------- {}", request);
        return procDefineService.getJsonList(request, pageSize, pageIndex);
    }

    /**
     * 获取流程图json
     * @param request 请求
     * @return Result
     */
    @PostMapping("/getJson")
    public Result getJson(@RequestBody ProcDefineJsonRequest request) {
        log.info("---------- 获取流程图json ---------- {}", request);
        return procDefineService.getJson(request);
    }

    /**
     * 查询流程定义列表
     * @param pageIndex 页码
     * @param pageSize 页数
     * @param request 请求
     * @return Result
     */
    @RequestMapping("/list")
    public Result list(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                       @RequestBody ProcDefineQueryRequest request) {
        log.info("---------- 查询流程定义列表 ---------- {}", request);
        return procDefineService.getDeployList(request, pageSize, pageIndex);
    }

    /**
     * 删除流程定义
     * @param request request
     * @return Result
     */
    @PostMapping("deleteProcess")
    public Result deleteProcess(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 删除流程定义 ---------- {}", request);
        procDefineService.deleteProcess(request);
        return Result.ok("删除流程定义成功");
    }

    /**
     * 使用当前流程版本
     * @param request request
     * @return Result
     */
    @PostMapping("/use")
    public Result use(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 使用当前流程版本 ---------- {}", request);
        procDefineService.use(request);
        return Result.ok("修改成功");
    }

    /**
     * 修改版本
     * @param request request
     * @return Result
     */
    @PostMapping("/editDescription")
    public Result editDescription(@RequestBody ProcDefineJsonRequest request) {
        log.info("---------- 使用当前流程版本 ---------- {}", request);
        procDefineService.editDescription(request);
        return Result.ok("修改成功");
    }


    @RequestMapping("/downModel")
    public Result downModel(@RequestParam(value = "defineId") String defineId, HttpServletResponse response) {
        log.info("---------- 下载流程定义 ---------- {}", defineId);
        procDefineService.downModel(defineId, response);
        return Result.OK_EMPTY;
    }
}